package com.huang.community.service.Imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.community.filter.SensitiveFilter;
import com.huang.community.mapper.MessageMapper;
import com.huang.community.pojo.Message;
import com.huang.community.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;
/**
 * 消息 服务类
 * @author TianTian
 * @date 2022/4/28 22:28
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper,Message> implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
   @Autowired
   private SensitiveFilter sensitiveFilter;
    //查询此userid的会话
    public List<Message> findConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId, offset, limit);
    }
    //查询会话条数
    public int findConversationCount(int userId) {
        return messageMapper.selectConversationCount(userId);
    }
    //通过会话id查询消息
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset, limit);
    }
    //查询消息条数
    public int findLetterCount(String conversationId) {
        return messageMapper.selectLetterCount(conversationId);
    }
    //查询未读消息
    public int findLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }
    //设置消息为已读
    public int readMessage(List<Integer> ids) {
        return messageMapper.updateStatus(ids, 1);
    }
    //增加消息
    public int addMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insert(message);
    }
    //查找最后发送的通知
    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.selectLatestNotice(userId, topic);
    }
    //查询通知总条数
    public int findNoticeCount(int userId, String topic) {
        return messageMapper.selectNoticeCount(userId, topic);
    }
    //查询未读总条数
    public int findNoticeUnreadCount(int userId, String topic) {
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }
    //查询通知
    public List<Message> findNotices(int userId, String topic, int offset, int limit) {
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }


}
